﻿/// <reference path="Panel.js" />

//可拖曳的面版
IM.Control.DragablePanel = function(conf) {
    IM.Control.Panel.apply(this, arguments);
    var _canDrag = true; //是否可拖曳
    var _canMove = false; //是否可以移动
    var _offset; //鼠标点击时与当前面版的座标偏移
    this.DragHandler; //鼠标拖曳点击区域对象

    //设置是否可能拖曳
    this.SetDrag = function(state) {
        if (typeof state != "boolean")
            return;

        _canDrag = state;
        //如果为静态元素，改为绝对
        if (state) {
            this.SetPosition("absolute");
        }
    };

    //拖曳层初始化
    this.DragablePanelInit = function() {
        if (!this.DragHandler)
            return;

        if (typeof this.DragHandler == "undefined" || this.DragHandler == null) {//如果DragHandler未定义则退出
            return;
        }

        if (typeof this.DragHandler == "string") {//如果传的是字符串id则用jquery获取对象
            this.DragHandler = $("#" + this.DragHandler);
        } else {
            this.DragHandler = $(this.DragHandler);
        }

        var pleft = 0;
        var ptop = 0;

        IM.Mouse.Initial();

        this.SetDrag(true);

        var startSelect = function() { return false; };

        //当鼠标弹起时
        var onmouseup = function() {
            IM.DetachEvent(document.body, "onselectstart", startSelect);
            _canMove = false;
        };
        //当鼠标移动时
        var onmousemove = (function(obj) {
            return function() {
                if (_canMove && _canDrag) {
                    //document.title = _offset.X;
                    obj.SetLeft(IM.Mouse.Location.X - _offset.X - pleft);
                    obj.SetTop(IM.Mouse.Location.Y - _offset.Y - ptop);

                    if (typeof obj.Container != "undefined") {//用于多文档窗体座标计算
                        if (typeof obj.Container.offset != "undefined") {
                            pleft = obj.Container.offset().left;
                            ptop = obj.Container.offset().top;
                        }
                        else if (typeof obj.Container.Container != "undefined") {
                            pleft = obj.Container.GetLeft();
                            ptop = obj.Container.GetTop();
                        }
                    }

                }
            };
        })(this);

        //当鼠标按下时
        var onmousedown = (function(obj) {
            return function() {
                IM.AttachEvent(document.body, "onselectstart", startSelect);
                _offset = {
                    X: IM.Mouse.Location.X - obj.GetLeft(),
                    Y: IM.Mouse.Location.Y - obj.GetTop()
                };

                _canMove = true;
            };
        })(this);

        //为窗体绑定鼠标移动事件
        $(document.body).bind("mousemove", onmousemove);
        //为拖曳手柄绑定鼠标弹起事件
        this.DragHandler.bind("mouseup", onmouseup);
        //为拖曳手柄绑定鼠标按下事件
        this.DragHandler.bind("mousedown", onmousedown);
    };

    this.DragablePanelInit();
};